package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.entity.User;
import com.util.JdbcUtil;

public class UserDaoImpl implements IUserDao {

	@Override
	public User login(String name, String password,int type) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "select * from user_list where user_name=? and user_password=? and user_type=?";
		User user = null;
		try {
			con = JdbcUtil.getConnection();
			ps = con.prepareStatement(sql);
			
			ps.setString(1, name);
			ps.setString(2, password);
			ps.setInt(3, type);
			rs = ps.executeQuery();
			if(rs.next()){
				user = new User(rs.getString("user_id"),rs.getString("user_name"),rs.getString("user_password"),rs.getString("user_address"),rs.getString("user_phone"),rs.getInt("user_type"));
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JdbcUtil.close(rs, ps, con);
		}
		return user;
	}

	@Override
	public void addUser(User user) {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "insert into user_list(user_id,user_name,user_password,user_address,user_phone,user_type) values(?,?,?,?,?,?)";
		try {
			con = JdbcUtil.getConnection();
			ps = con.prepareStatement(sql);
			ps.setString(1, user.getId());
			ps.setString(2, user.getName());
			ps.setString(3, user.getPassword());
			ps.setString(4, user.getPhone());
			ps.setString(5, user.getAddress());
			ps.setInt(6, user.getType());
			ps.executeUpdate();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JdbcUtil.close(rs, ps, con);
		}
	}

	@Override
	public List<User> findAllUsers() {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "select * from user_list";
		List<User> users = new ArrayList<User>();
		try {
			con = JdbcUtil.getConnection();
			ps = con.prepareStatement(sql);
			rs = ps.executeQuery();
			while(rs.next()){
				User user = new User(rs.getString("user_id"),rs.getString("user_name"),rs.getString("user_password"),rs.getString("user_address"),rs.getString("user_phone"),rs.getInt("user_type"));
				users.add(user);
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JdbcUtil.close(rs, ps, con);
		}
		return users;
	}
	public void deleteUserById(String id) {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "delete from user_list where user_id=?";
		try {
			con = JdbcUtil.getConnection();
			ps = con.prepareStatement(sql);
			ps.setString(1, id);
			int i = ps.executeUpdate();
			System.out.println(i+"��ɾ��ɹ�");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JdbcUtil.close(rs, ps, con);
		}
	}

	public void updateUser(User user) {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql ="update user_list set user_name=?,user_password=?,user_address=?,user_phone=?,user_type=? where user_id=?";
		
		try {
			con = JdbcUtil.getConnection();
			ps = con.prepareStatement(sql);
			ps.setString(1, user.getName());
			ps.setString(2, user.getPassword());
			ps.setString(3, user.getAddress());
			ps.setString(4, user.getPhone());
			ps.setInt(5, user.getType());
			ps.setString(6,user.getId());
			ps.executeUpdate();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			}finally{
			JdbcUtil.close(rs, ps, con);
		}
	}

}
